//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.gui;

import javax.swing.*;

import ants.p2p.*;
import ants.p2p.filesharing.*;
import ants.p2p.query.*;
import ants.p2p.query.security.*;
import ants.p2p.security.*;
import ants.p2p.utils.indexer.*;
import java.awt.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.Toolkit;
import java.awt.datatransfer.*;

import org.apache.log4j.*;
import java.io.*;

public class AntPopupMenu extends JPopupMenu {
  QueryAntPanel callerQuery;
  DownloadAntPanel callerDownload;
  UploadAntPanel callerUpload;
  JPanel caller;
  FilePullMessageProcessor toBeInterrupted;
  QueryFileTuple fileToDownload;
  QueryFileTuple source;
  QueryFileTuple[] sources;
  InterruptedDownload toBeProcessed;
  MultipleSourcesDownloadManager downloader;
  JMenuItem jMenuItem1 = new JMenuItem();
  int selRow;
  TreePath selPath;
  JMenuItem jMenuItem3 = new JMenuItem();
  JMenuItem jMenuItem4 = new JMenuItem();
  JMenuItem jMenuItem5 = new JMenuItem();
  JMenuItem jMenuItem6 = new JMenuItem();
  JMenuItem jMenuItem7 = new JMenuItem();
  JMenuItem jMenuItem8 = new JMenuItem();

  static Logger _logger = Logger.getLogger(AntPopupMenu.class.getName());
  JMenuItem jMenuItem9 = new JMenuItem();
  JMenuItem jMenuItem10 = new JMenuItem();
  JMenuItem jMenuItem11 = new JMenuItem();
  JMenuItem jMenuItem12 = new JMenuItem();
  JMenuItem jMenuItem13 = new JMenuItem();
  JMenuItem jMenuItem14 = new JMenuItem();
  JMenuItem jMenuItem15 = new JMenuItem();

  public AntPopupMenu(QueryAntPanel callerQuery, QueryFileTuple fileToDownload, QueryFileTuple[] sources) {
    super();
    this.sources = sources;
    this.selPath = selPath;
    this.selRow = selRow;
    this.fileToDownload = fileToDownload;
    this.callerQuery = callerQuery;
    this.caller = caller;
    this.addPropertyChangeListener(this.callerQuery.container.dap);
    try {
      jbInit();
    }
    catch(Exception e) {
      _logger.error("", e);
    }
  }

  public AntPopupMenu(DownloadAntPanel caller, MultipleSourcesDownloadManager downloader,
                      int selRow, TreePath selPath) {
    super();
    this.selPath = selPath;
    this.selRow = selRow;
    this.downloader = downloader;
    this.callerDownload = (DownloadAntPanel)caller;
    this.caller = caller;
    this.addPropertyChangeListener(this.callerDownload);
    try {
      jbInit();
    }
    catch (Exception e) {
      _logger.error("", e);
    }
  }

  public AntPopupMenu(DownloadAntPanel caller, QueryFileTuple source, MultipleSourcesDownloadManager downloader,
                      int selRow, TreePath selPath) {
    super();
    this.selPath = selPath;
    this.selRow = selRow;
    this.downloader = downloader;
    this.source = source;
    this.callerDownload = (DownloadAntPanel)caller;
    this.caller = caller;
    this.addPropertyChangeListener(this.callerDownload);
    try {
      jbInit();
    }
    catch (Exception e) {
      _logger.error("", e);
    }
  }

  public AntPopupMenu(DownloadAntPanel caller, InterruptedDownload toBeProcessed,
                      int selRow, TreePath selPath) {
    super();
    this.selPath = selPath;
    this.selRow = selRow;
    this.toBeProcessed = toBeProcessed;
    this.callerDownload = (DownloadAntPanel)caller;
    this.caller = caller;
    try {
      jbInit();
    }
    catch (Exception e) {
      _logger.error("", e);
    }
  }

  public AntPopupMenu(UploadAntPanel caller, FilePullMessageProcessor toBeInterrupted,
                      int selRow, TreePath selPath) {
    super();
    this.selPath = selPath;
    this.selRow = selRow;
    this.toBeInterrupted = toBeInterrupted;
    this.callerUpload = (UploadAntPanel)caller;
    this.caller = caller;

    try {
      jbInit();
    }
    catch (Exception e) {
      _logger.error("", e);
    }
  }

  private void jbInit() throws Exception {
    jMenuItem1.setActionCommand(ji.JI.i("Secure connection"));
    jMenuItem1.setText(ji.JI.i("Download File (Create secure connection)"));
    jMenuItem1.addMouseListener(new AntPopupMenu_jMenuItem1_mouseAdapter(this));
    jMenuItem3.setText(ji.JI.i("Clear"));
    jMenuItem3.addMouseListener(new AntPopupMenu_jMenuItem3_mouseAdapter(this));
    jMenuItem4.setText(ji.JI.i("Interrupt"));
    jMenuItem4.addMouseListener(new AntPopupMenu_jMenuItem4_mouseAdapter(this));
    jMenuItem5.setText(ji.JI.i("Remove source"));
    jMenuItem5.addMouseListener(new AntPopupMenu_jMenuItem5_mouseAdapter(this));
    jMenuItem6.setText(ji.JI.i("Search sources"));
    jMenuItem6.addMouseListener(new AntPopupMenu_jMenuItem6_mouseAdapter(this));
    jMenuItem7.setText(ji.JI.i("Interrupt"));
    jMenuItem7.addMouseListener(new AntPopupMenu_jMenuItem7_mouseAdapter(this));
    jMenuItem8.setText(ji.JI.i("Remove Interrupted"));
    jMenuItem8.addMouseListener(new AntPopupMenu_jMenuItem8_mouseAdapter(this));
    jMenuItem9.setText(ji.JI.i("Activate Autoresume"));
    jMenuItem9.addMouseListener(new AntPopupMenu_jMenuItem9_mouseAdapter(this));
    jMenuItem10.setText(ji.JI.i("Schedule Download"));
    jMenuItem10.addMouseListener(new AntPopupMenu_jMenuItem10_mouseAdapter(this));
    jMenuItem11.setText(ji.JI.i("Copy the ed2k link in the clipboard"));
    jMenuItem11.addMouseListener(new AntPopupMenu_jMenuItem11_mouseAdapter(this));
    jMenuItem12.setText(ji.JI.i("Open the links html page"));
    jMenuItem12.addMouseListener(new AntPopupMenu_jMenuItem12_mouseAdapter(this));
    jMenuItem13.setText(ji.JI.i("Copy the ants2k link in the clipboard"));
    jMenuItem13.addMouseListener(new AntPopupMenu_jMenuItem13_mouseAdapter(this));
    jMenuItem14.setText(ji.JI.i("Preview"));
    jMenuItem14.addMouseListener(new AntPopupMenu_jMenuItem14_mouseAdapter(this));
    jMenuItem15.setText(ji.JI.i("Private chat"));
    jMenuItem15.addMouseListener(new AntPopupMenu_jMenuItem15_mouseAdapter(this));
    this.add(jMenuItem1);
    this.add(jMenuItem3);
    this.add(jMenuItem4);
    this.add(jMenuItem5);
    this.add(jMenuItem6);
    this.add(jMenuItem7);
    this.add(jMenuItem8);
    this.add(jMenuItem9);
    this.add(jMenuItem10);

    if(this.callerQuery != null){
      this.remove(jMenuItem3);
      this.remove(jMenuItem4);
      this.remove(jMenuItem5);
      this.remove(jMenuItem6);
      this.remove(jMenuItem7);
      this.remove(jMenuItem8);
      this.remove(jMenuItem9);
      EndpointSecurityManager esman = this.callerQuery.container.cap.warriorAnt.getOutputSecureConnectionManager(fileToDownload.getOwnerID());
      if (esman == null) {
        this.jMenuItem1.setText(ji.JI.i("Download File (Create secure connection)") + ((fileToDownload.getOwnerID() != null) ? " " + ji.JI.i("from")+ " " + fileToDownload.getOwnerID().substring(0,10) : ""));
      }else{
        this.jMenuItem1.setText(ji.JI.i("Download File")+ ((fileToDownload.getOwnerID() != null) ? " " + ji.JI.i("from")+ " " + fileToDownload.getOwnerID().substring(0,10) : ""));
      }
      if(fileToDownload.getOwnerID() == null){
        this.remove(jMenuItem1);
        if(BackgroundEngine.getInstance().getLocalFile(fileToDownload.getFileHash(), QueryHashItem.ANTS_HASH) != null){
          this.remove(jMenuItem10);
        }else
          this.addSeparator();
        this.add(jMenuItem11);
        this.add(jMenuItem13);
        this.add(jMenuItem12);
      }else{
        if(BackgroundEngine.getInstance().getLocalFile(fileToDownload.getFileHash(), QueryHashItem.ANTS_HASH) != null){
          this.remove(jMenuItem1);
          this.remove(jMenuItem10);
        }else
          this.addSeparator();
        this.add(jMenuItem11);
        this.add(jMenuItem13);
        this.add(jMenuItem12);
        EndpointSecurityManager esm = this.callerQuery.container.cap.warriorAnt.
            getOutputSecureConnectionManager(fileToDownload.getOwnerID());

        if (esm == null) {
          this.remove(jMenuItem10);
          DefaultMutableTreeNode[] msdmInterrupted = new
              DefaultMutableTreeNode[this.callerQuery.container.dap.
              interruptedRoot.
              getChildCount()];
          DefaultMutableTreeNode[] msdmInDownload = new
            DefaultMutableTreeNode[this.callerQuery.container.dap.
            downloadRoot.
            getChildCount()];
          for (int x = 0; x < msdmInterrupted.length; x++) {
            msdmInterrupted[x] = (DefaultMutableTreeNode)this.callerQuery.
                container.
                dap.interruptedRoot.getChildAt(x);
          }
          for (int x = 0; x < msdmInDownload.length; x++) {
            msdmInDownload[x] = (DefaultMutableTreeNode)this.callerQuery.
                container.
                dap.downloadRoot.getChildAt(x);
          }
          for (int x = 0; x < msdmInterrupted.length; x++) {
            InterruptedDownload currentNode = (
                InterruptedDownload) msdmInterrupted[x].
                getUserObject();
            if (currentNode.getFileHash().equals(fileToDownload.getFileHash())) {
              jMenuItem1.setText(ji.JI.i("Resume (Create secure connection)")+((fileToDownload.getOwnerID() != null) ? " " + ji.JI.i("from")+ " " + fileToDownload.getOwnerID().substring(0,10) : ""));
            }
          }
          for (int x = 0; x < msdmInDownload.length; x++) {
            MultipleSourcesDownloadManager currentNode = (
                MultipleSourcesDownloadManager) msdmInDownload[x].
                getUserObject();
            if (currentNode.getFileHash().equals(fileToDownload.getFileHash())) {
              jMenuItem1.setText(ji.JI.i("Resume (Create secure connection)")+((fileToDownload.getOwnerID() != null) ? " " + ji.JI.i("from")+ " " + fileToDownload.getOwnerID().substring(0,10) : ""));
            }
          }
        }
        else {
          this.remove(jMenuItem10);
          DefaultMutableTreeNode[] msdmInterrupted = new
              DefaultMutableTreeNode[this.callerQuery.container.dap.
              interruptedRoot.
              getChildCount()];
          DefaultMutableTreeNode[] msdmInDownload = new
            DefaultMutableTreeNode[this.callerQuery.container.dap.
            downloadRoot.
            getChildCount()];
          for (int x = 0; x < msdmInterrupted.length; x++) {
            msdmInterrupted[x] = (DefaultMutableTreeNode)this.callerQuery.
                container.
                dap.interruptedRoot.getChildAt(x);
          }
          for (int x = 0; x < msdmInDownload.length; x++) {
            msdmInDownload[x] = (DefaultMutableTreeNode)this.callerQuery.
                container.
                dap.downloadRoot.getChildAt(x);
          }
          for (int x = 0; x < msdmInterrupted.length; x++) {
            InterruptedDownload currentNode = (
                InterruptedDownload) msdmInterrupted[x].
                getUserObject();
            if (currentNode.getFileHash().equals(fileToDownload.getFileHash())) {
              jMenuItem1.setText(ji.JI.i("Resume") +((fileToDownload.getOwnerID() != null) ? " " + ji.JI.i("from")+ " " + fileToDownload.getOwnerID().substring(0,10) : ""));
            }
          }
          for (int x = 0; x < msdmInDownload.length; x++) {
            MultipleSourcesDownloadManager currentNode = (
                MultipleSourcesDownloadManager) msdmInDownload[x].
                getUserObject();
            if (currentNode.getFileHash().equals(fileToDownload.getFileHash())) {
              jMenuItem1.setText(ji.JI.i("Resume") +((fileToDownload.getOwnerID() != null) ? " " + ji.JI.i("from")+ " " + fileToDownload.getOwnerID().substring(0,10) : ""));
            }
          }
        }
      }
      this.addSeparator();
      JMenu sourcesMenu = new JMenu(ji.JI.i("Sources"));
      for(int x = 0;x < this.sources.length && x < 10; x++){
        if(sources[x].getOwnerID() != null){
          String percentage = "   100%";
          if(sources[x] instanceof QueryPartialFileTuple){
            QueryPartialFileTuple qpft = (QueryPartialFileTuple)sources[x];
            percentage = "   "+qpft.getPercentage()+"   "+ji.JI.i("Slots: ")+qpft.getFreeSlots();
          }else if(sources[x] instanceof QueryCompletedFileTuple){
            QueryCompletedFileTuple qcft = (QueryCompletedFileTuple)sources[x];
            percentage += "   "+ji.JI.i("Slots: ")+qcft.getFreeSlots()+"   "+qcft.getComment();
          }else if(sources[x] instanceof QueryRemoteFileTuple){
            QueryRemoteFileTuple qrft = (QueryRemoteFileTuple)sources[x];
            percentage = "   Remote";
          }
          JMenuItem item = new JMenuItem(sources[x].getOwnerID().substring(0,10) + percentage);
          item.addMouseListener(new AntPopupMenu_sourceItem_mouseAdapter(this));
          sourcesMenu.add(item);
        }
        else{
          JMenuItem item = new JMenuItem(ji.JI.i("No Owners"));
          sourcesMenu.add(item);
        }
      }
      this.add(sourcesMenu);
      if(BackgroundEngine.getInstance().getLocalFile(fileToDownload.getFileHash(), QueryHashItem.ANTS_HASH) != null){
        this.addSeparator();
        JMenuItem openItem = new JMenuItem(ji.JI.i("Copy File Path to Clipboard"));
        openItem.addMouseListener(new AntPopupMenu_openFile_mouseAdapter(this));
        this.add(openItem);
      }
    }
    if(this.callerDownload != null){
      this.add(jMenuItem15);
      this.addSeparator();
      this.add(jMenuItem11);
      this.add(jMenuItem13);
      this.add(jMenuItem12);
      this.remove(jMenuItem1);
      this.remove(jMenuItem10);
      this.remove(jMenuItem7);
      if(source != null){
        this.remove(jMenuItem3);
        this.remove(jMenuItem4);
        this.remove(jMenuItem6);
        this.remove(jMenuItem8);
        this.remove(jMenuItem9);
      }else if(downloader!=null){
        this.remove(jMenuItem15);
        if (downloader.isAlive()) {
          this.remove(jMenuItem3);
          this.remove(jMenuItem5);
          this.remove(jMenuItem8);
          if(downloader.isAutoresumeActive())
            this.jMenuItem9.setText(ji.JI.i("Deactivate Autoresume"));
          this.add(jMenuItem14);
        }
        else {
          if(!downloader.checkCompleted())
            this.jMenuItem3.setEnabled(false);
          this.remove(jMenuItem4);
          this.remove(jMenuItem5);
          this.remove(jMenuItem6);
          this.remove(jMenuItem8);
          this.remove(jMenuItem9);
        }
      }else if(toBeProcessed!=null){
        this.remove(jMenuItem15);
        this.remove(jMenuItem3);
        this.remove(jMenuItem4);
        this.remove(jMenuItem5);
        if(toBeProcessed.isAutoresumeActive())
            this.jMenuItem9.setText(ji.JI.i("Deactivate Autoresume"));
        this.add(jMenuItem14);
      }
    }
    if(this.callerUpload != null){
      this.remove(jMenuItem1);
      this.remove(jMenuItem10);
      this.remove(jMenuItem3);
      this.remove(jMenuItem4);
      this.remove(jMenuItem5);
      this.remove(jMenuItem6);
      this.remove(jMenuItem8);
      this.remove(jMenuItem9);
      this.add(jMenuItem15);
    }
  }

  void jMenuItem1_mousePressed(MouseEvent e) {
    if (this.fileToDownload != null) {
      EndpointSecurityManager esm = this.callerQuery.container.cap.warriorAnt.
            getOutputSecureConnectionManager(fileToDownload.getOwnerID());
      if (esm == null) {
        this.callerQuery.container.dap.createInterruptedDownload(this.
            fileToDownload);
        this.callerQuery.container.cap.warriorAnt.scheduleDownload(this.
            fileToDownload);
        this.callerQuery.container.cap.warriorAnt.createSecureConnection(this.
            fileToDownload.getOwnerID(),
            false);//! (this.fileToDownload instanceof QueryRemoteFileTuple));
        this.setVisible(false);
      }else{
        this.callerQuery.container.dap.createInterruptedDownload(this.
            fileToDownload);
        this.firePropertyChange("addSourcePeer",null,this.fileToDownload);
        this.setVisible(false);
      }
    }
  }

  void jMenuItem3_mousePressed(MouseEvent e) {
    DefaultMutableTreeNode toBeRemoved = ((DefaultMutableTreeNode) selPath.getLastPathComponent());
    this.callerDownload.downloadTree.removeNodeFromParent(toBeRemoved);
    this.callerDownload.jTree1.setModel(this.callerDownload.downloadTree);
    //this.callerDownload.downloadTree.reload();
    this.callerDownload.repaint();
  }

  void jMenuItem4_mousePressed(MouseEvent e) {
    DefaultMutableTreeNode toBeRemoved = ((DefaultMutableTreeNode) selPath.getLastPathComponent());
    MultipleSourcesDownloadManager msdm = (MultipleSourcesDownloadManager)toBeRemoved.getUserObject();
    msdm.removeAllPeers(true);
    /*this.callerDownload.downloadTree.removeNodeFromParent(toBeRemoved);
    this.callerDownload.jTree1.setModel(this.callerDownload.downloadTree);
    this.callerDownload.downloadTree.reload();
    this.callerDownload.repaint();
    InterruptedDownload interruptedDownload = new
        InterruptedDownload(msdm);
    if (msdm.isAutoresumeActive()) {
      msdm.getAutoresumeEngine().setInterruptedDownload(
          interruptedDownload);
    }
    this.callerDownload.interruptedRoot.add(new DefaultMutableTreeNode(interruptedDownload));
    this.callerDownload.jTree2.setModel(this.callerDownload.interruptedTree);
    this.callerDownload.interruptedTree.reload();
    this.callerDownload.repaint();*/
  }

  void jMenuItem5_mousePressed(MouseEvent e) {
    DefaultMutableTreeNode toBeRemoved = ((DefaultMutableTreeNode) selPath.getLastPathComponent());
    DefaultMutableTreeNode father = ((DefaultMutableTreeNode) selPath.getParentPath().getLastPathComponent());
    MultipleSourcesDownloadManager msdm = (MultipleSourcesDownloadManager)father.getUserObject();
    QueryFileTuple qft = (QueryFileTuple)toBeRemoved.getUserObject();
    msdm.propertyChangeSupport.firePropertyChange(
            "removedSourcePeer", msdm, qft.getOwnerID());
  }

  void jMenuItem6_mousePressed(MouseEvent e) {
    if(this.toBeProcessed!=null){
      DefaultMutableTreeNode selected = ( (DefaultMutableTreeNode) selPath.
                                         getLastPathComponent());
      InterruptedDownload toBeResumed = (InterruptedDownload) selected.
          getUserObject();
      QueryAntPanel qap = new QueryAntPanel(this.callerDownload.container);
      qap.jTextField1.setText("ants2k://|file|"+toBeResumed.getFileName()+"|"+toBeResumed.getFileSize()+"|"+toBeResumed.getFileHash()+"|/");
      this.callerDownload.container.queryPanels.add(qap);
      int max = toBeResumed.getFileName().length();
      if (max > 10)
        max = 10;
      this.callerDownload.container.jTabbedPane1.add(qap, new LabelIcon(ji.JI.i("Search")+": " +
          toBeResumed.getFileHash().substring(0, max), DoubleBuffer.getInstance().getButton(12), this));
      this.callerDownload.container.cap.warriorAnt.getPropertyChangeSupport().
          addPropertyChangeListener(qap);
      try {
        qap.searchByHash(true);
      }
      catch (Exception ex) {
        _logger.error("",ex);
      }
    }else if(this.downloader!=null){
      DefaultMutableTreeNode selected = ( (DefaultMutableTreeNode) selPath.
                                         getLastPathComponent());
      MultipleSourcesDownloadManager downloader = (MultipleSourcesDownloadManager) selected.
          getUserObject();
      QueryAntPanel qap = new QueryAntPanel(this.callerDownload.container);
      qap.jTextField1.setText("ants2k://|file|"+downloader.getFileName()+"|"+downloader.getFileSize()+"|"+downloader.getFileHash()+"|/");
      this.callerDownload.container.queryPanels.add(qap);
      int max = downloader.getFileName().length();
      if (max > 10)
        max = 10;
      this.callerDownload.container.jTabbedPane1.add(qap, new LabelIcon(ji.JI.i("Search")+": " +
          downloader.getFileHash().substring(0, max), DoubleBuffer.getInstance().getButton(12), this));
      this.callerDownload.container.cap.warriorAnt.getPropertyChangeSupport().
          addPropertyChangeListener(qap);
      try {
        qap.searchByHash(true);
      }
      catch (Exception ex) {
        _logger.error("",ex);
      }
    }
  }

  void jMenuItem7_mousePressed(MouseEvent e) {
    DefaultMutableTreeNode selected = ((DefaultMutableTreeNode) selPath.getLastPathComponent());
    FilePullMessageProcessor toBeInterrupted = (FilePullMessageProcessor)selected.getUserObject();
    toBeInterrupted.terminate();
    this.callerUpload.uploadTree.removeNodeFromParent(selected);
    this.callerUpload.jTree1.setModel(this.callerUpload.uploadTree);
    this.callerUpload.uploadTree.reload();
    this.callerUpload.repaint();
  }

  void jMenuItem8_mousePressed(MouseEvent e) {
    if (this.toBeProcessed != null) {
      DefaultMutableTreeNode selected = ( (DefaultMutableTreeNode) selPath.
                                         getLastPathComponent());
      InterruptedDownload toBeRemoved = (InterruptedDownload) selected.
          getUserObject();
      toBeRemoved.deactivateAutoresumeEngine();
      toBeRemoved.removeInterrupted();
      this.callerDownload.container.cap.warriorAnt.unscheduleDownload(toBeRemoved.getFileHash());
      this.callerDownload.container.dap.interruptedTree.removeNodeFromParent(selected);
      this.callerDownload.container.dap.jTree2.setModel(this.callerDownload.
          container.dap.interruptedTree);
      this.callerDownload.container.dap.repaint();
    }
  }

  void jMenuItem9_mousePressed(MouseEvent e) {
    if(this.downloader != null){
      if(this.downloader.isAutoresumeActive()){
        this.downloader.deactivateAutoresumeEngine();
      }
      else{
        AutoresumeEngine ae = new AutoresumeEngine(this.downloader, this.callerDownload);
      }
    }else if(this.toBeProcessed != null){
      if(this.toBeProcessed.isAutoresumeActive()){
        this.toBeProcessed.deactivateAutoresumeEngine();
      }
      else{
        AutoresumeEngine ae = new AutoresumeEngine(this.toBeProcessed, this.callerDownload.container.cap.warriorAnt, this.callerDownload);
      }
    }
  }

  void jMenuItem10_mousePressed(MouseEvent e) {
    if(this.fileToDownload !=null){
      this.firePropertyChange("addSourcePeer", null, this.fileToDownload);
      this.setVisible(false);
    }
  }

  void sourceItem_mousePressed(MouseEvent e) {
    /*
    JMenuItem sourceItem = (JMenuItem) e.getSource();
    for(int x = 0; x < this.sources.length; x++){
      if(this.sources[x].getOwnerID().substring(0,10).equals(sourceItem.getText().substring(0,10))){
        AntPopupMenu newPopup = new AntPopupMenu(this.callerQuery, this.sources[x], this.sources);
        Component invoker = this.getInvoker();
        int xPos = this.getX();
        int yPos = this.getY();
        this.setVisible(false);
        newPopup.show(invoker, xPos, yPos);
      }
    }
    */
  }

  void sourceItem_mouseEntered(MouseEvent e) {
    JMenuItem sourceItem = (JMenuItem) e.getSource();
    for(int x = 0; x < this.sources.length; x++){
      if(this.sources[x].getOwnerID().substring(0,10).equals(sourceItem.getText().substring(0,10))){
        this.fileToDownload = this.sources[x];
        EndpointSecurityManager esm = this.callerQuery.container.cap.warriorAnt.
            getOutputSecureConnectionManager(fileToDownload.getOwnerID());
        if (esm == null) {
          jMenuItem1.setText(ji.JI.i("Download File (Create secure connection)"));
        }else{
          jMenuItem1.setText(ji.JI.i("Download File"));
        }
        jMenuItem1.setText(this.jMenuItem1.getText()+ ((fileToDownload.getOwnerID() != null) ? " " + ji.JI.i("from")+ " " + fileToDownload.getOwnerID().substring(0,10) : ""));
      }
    }
  }

  void clipboardCopy_mousePressed(MouseEvent e) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection data = new StringSelection("ants2k://|file|"+this.fileToDownload.getFileName()+"|"+this.fileToDownload.getSize()+"|"+this.fileToDownload.getFileHash()+"|/");
    clipboard.setContents(data, data);
    this.setVisible(false);
  }

  void openFile_mousePressed(MouseEvent e) {
    FileInfos fi = BackgroundEngine.getInstance().getLocalFile(fileToDownload.getFileHash(), QueryHashItem.ANTS_HASH);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection data = new StringSelection(fi.getName());
    clipboard.setContents(data, data);
    this.setVisible(false);
  }

  void jMenuItem11_mousePressed(MouseEvent e) {
    String fileName = null;
    String fileHash = null;
    long fileSize = 0;
    if(this.fileToDownload != null){
      fileName = this.fileToDownload.getFileName();
      fileHash = this.fileToDownload.getED2KFileHash();
      fileSize = this.fileToDownload.getSize().longValue();
    }else if(this.downloader != null){
      fileName = this.downloader.getFileName();
      fileHash = this.downloader.getED2KFileHash();
      fileSize = this.downloader.getFileSize();
    }else if(this.toBeProcessed != null){
      fileName = this.toBeProcessed.getFileName();
      fileHash = this.toBeProcessed.getED2KFileHash();
      fileSize = this.toBeProcessed.getFileSize();
    }
    if(fileName != null && fileHash != null){
      Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
      StringSelection data = new StringSelection("ed2k://|file|" + fileName +
                                                 "|" + fileSize +
                                                 "|" +
                                                 fileHash +
                                                 "|/");
      clipboard.setContents(data, data);
    }
    this.setVisible(false);
  }

  void jMenuItem12_mousePressed(MouseEvent e) {
    String fileName = null;
    String ants2kfileHash = null;
    String ed2kFileHash = null;
    String extendedInfos = null;
    long fileSize = 0;
    if(this.fileToDownload != null){
      fileName = this.fileToDownload.getFileName();
      ed2kFileHash = this.fileToDownload.getED2KFileHash();
      ants2kfileHash = this.fileToDownload.getFileHash();
      fileSize = this.fileToDownload.getSize().longValue();
      extendedInfos = this.fileToDownload.getExtendedInfos();
    }else if(this.downloader != null){
      fileName = this.downloader.getFileName();
      ed2kFileHash = this.downloader.getED2KFileHash();
      ants2kfileHash = this.downloader.getFileHash();
      fileSize = this.downloader.getFileSize();
      extendedInfos = this.downloader.getExtendedInfos();
    }else if(this.toBeProcessed != null){
      fileName = this.toBeProcessed.getFileName();
      ed2kFileHash = this.toBeProcessed.getED2KFileHash();
      ants2kfileHash = this.toBeProcessed.getFileHash();
      fileSize = this.toBeProcessed.getFileSize();
      extendedInfos = this.toBeProcessed.getExtendedInfos();
    }
    if(fileName != null && ed2kFileHash != null && ants2kfileHash != null){
      String htmlPage = "<html><head><title>"+ji.JI.i("ANts P2P Links Page")+"</title></head><body bgcolor=\"#000000\"><p><table style=\"color: #0066CC; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt;\"><tr><td><img src=\"ed2k_1.gif\"/><td>"+fileName+"</tr><tr><td><td>"+extendedInfos+"</tr><tr><td><img src=\"ed2k_2.gif\"/><td><a href=\"ed2k://|file|"+fileName+"|"+fileSize+"|"+ed2kFileHash+"|/\">ed2k://|file|"+fileName+"|"+fileSize+"|"+ed2kFileHash+"|/</a></tr><tr><td><img src=\"ed2k_2.gif\"/><td><a href=\"ants2k://|file|"+fileName+"|"+fileSize+"|"+ants2kfileHash+"|/\">ants2k://|file|"+fileName+"|"+fileSize+"|"+ants2kfileHash+"|/</a></tr></p>";
      String[] args = new String[1];
      try{
        WarriorAnt.checked2kPath();
        PrintWriter pw = new PrintWriter(new FileWriter(WarriorAnt.ed2kPath + fileName+".html"));
        pw.write(htmlPage);
        pw.close();
        args[0] = (new File(WarriorAnt.ed2kPath + fileName + ".html")).getAbsolutePath();
        Runtime.getRuntime().exec("\""+HttpAntPanel.browserPath+"\""+" \""+args[0]+"\"");
      }catch(Exception ex){
        JOptionPane.showMessageDialog(this, ji.JI.i("Not found")+": "+HttpAntPanel.browserPath+"\n"+ji.JI.i("Please set your browser path in settings..."),ji.JI.i("Error in starting Browser"),JOptionPane.ERROR_MESSAGE);
        _logger.error("Error in starting: "+HttpAntPanel.browserPath+" "+args[0],ex);
      }
    }
    this.setVisible(false);
  }

  void jMenuItem13_mousePressed(MouseEvent e) {
    String fileName = null;
    String fileHash = null;
    long fileSize = 0;
    if(this.fileToDownload != null){
      fileName = this.fileToDownload.getFileName();
      fileHash = this.fileToDownload.getFileHash();
      fileSize = this.fileToDownload.getSize().longValue();
    }else if(this.downloader != null){
      fileName = this.downloader.getFileName();
      fileHash = this.downloader.getFileHash();
      fileSize = this.downloader.getFileSize();
    }else if(this.toBeProcessed != null){
      fileName = this.toBeProcessed.getFileName();
      fileHash = this.toBeProcessed.getFileHash();
      fileSize = this.toBeProcessed.getFileSize();
    }
    if(fileName != null && fileHash != null){
      Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
      StringSelection data = new StringSelection("ants2k://|file|" + fileName +
                                                 "|" + fileSize +
                                                 "|" +
                                                 fileHash +
                                                 "|/");
      clipboard.setContents(data, data);
    }
    this.setVisible(false);
  }

  void jMenuItem14_mousePressed(MouseEvent e) {
    String fileName = null;
    String ants2kfileHash = null;
    if(this.downloader != null){
      fileName = this.downloader.getFileName();
      ants2kfileHash = this.downloader.getFileHash();
    }else if(this.toBeProcessed != null){
      fileName = this.toBeProcessed.getFileName();
      ants2kfileHash = this.toBeProcessed.getFileHash();
    }
    int avaiablePreviewChunks = 0;
    if(fileName != null && ants2kfileHash != null &&
       ((downloader != null && (avaiablePreviewChunks = downloader.getAvaiablePreviewIndexes()) > 0 && !downloader.checkCompleted()) ||
       (toBeProcessed != null && (avaiablePreviewChunks = toBeProcessed.getAvaiablePreviewIndexes()) > 0))){
      this.callerDownload.container.cap.warriorAnt.checkPreviewPath();
      File f = new File(this.callerDownload.container.cap.warriorAnt.previewsPath+fileName);
      try{
        OutputStream os = new FileOutputStream(f);
        for(int x = 0; x < avaiablePreviewChunks; x++){
          File chunk = new File(this.callerDownload.container.cap.warriorAnt.chunksHome +
                                this.callerDownload.container.cap.warriorAnt.chunksPath+ants2kfileHash+"."+x);
          InputStream is = new FileInputStream(chunk);
          byte[] content = new byte[MultipleSourcesDownloadManager.blocksPerSource * WarriorAnt.blockSizeInDownload];
          while (is.available() > 0) {
            int read = is.read(content);
            if (read > 0) {
              if (read < content.length) {
                byte[] reduced = new byte[read];
                for (int g = 0; g < read; g++) {
                  reduced[g] = content[g];
                }
                content = reduced;
              }
              os.write(content);
            }
          }
          is.close();
        }
        os.close();
      }catch(Exception ex){
        JOptionPane.showMessageDialog(this, ji.JI.i("Cannot create preview for "+fileName),ji.JI.i("Error in creating preview file"),JOptionPane.ERROR_MESSAGE);
        _logger.error("Cannot create preview file "+fileName,ex);
        this.setVisible(false);
        return;
      }
      String args[] = new String[1];
      try{
        args[0] = f.getAbsolutePath();
        Runtime.getRuntime().exec("\"" + HttpAntPanel.browserPath + "\"" +
                                  " \"file://" + args[0] + "\"");
      }catch(Exception ex){
        JOptionPane.showMessageDialog(this, ji.JI.i("Not found")+": "+HttpAntPanel.browserPath+"\n"+ji.JI.i("Please set your browser path in settings..."),ji.JI.i("Error in starting Browser"),JOptionPane.ERROR_MESSAGE);
        _logger.error("Error in starting: "+HttpAntPanel.browserPath+" "+args[0],ex);
      }
    }else{
      JOptionPane.showMessageDialog(this, ji.JI.i("Cannot create preview for") + " "+fileName+": "+ji.JI.i("not enough chunks avaiable"),ji.JI.i("Error in creating preview file"),JOptionPane.ERROR_MESSAGE);
    }
    this.setVisible(false);
  }

  void jMenuItem15_mousePressed(MouseEvent e) {
    if(this.callerDownload != null){
      PrivateChatFrame pcf = PrivateChatFrame.getChatWith(source.getOwnerID(),
          this.callerDownload.container.cap.getWarriorAnt(), true);
      pcf.show();
      pcf.transferFocus();
    }else if(this.callerUpload != null){
      DefaultMutableTreeNode selected = ((DefaultMutableTreeNode) selPath.getLastPathComponent());
      FilePullMessageProcessor toBeInterrupted = (FilePullMessageProcessor)selected.getUserObject();
      PrivateChatFrame pcf = PrivateChatFrame.getChatWith(toBeInterrupted.getFilePullMessage().getSource(),
          this.callerUpload.container.cap.getWarriorAnt(), false);
      pcf.show();
      pcf.transferFocus();
    }
  }
}

class AntPopupMenu_jMenuItem1_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem1_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.jMenuItem1_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_jMenuItem3_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem3_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.jMenuItem3_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_jMenuItem4_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem4_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.jMenuItem4_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_jMenuItem5_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem5_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.jMenuItem5_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_jMenuItem6_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem6_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.jMenuItem6_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_jMenuItem7_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem7_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.jMenuItem7_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_jMenuItem8_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem8_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.jMenuItem8_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_jMenuItem9_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem9_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.jMenuItem9_mousePressed(e);
  }
}

class AntPopupMenu_jMenuItem10_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem10_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.jMenuItem10_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_sourceItem_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_sourceItem_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.sourceItem_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
  public void mouseEntered(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.sourceItem_mouseEntered(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_clipboardCopy_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_clipboardCopy_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.clipboardCopy_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_openFile_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_openFile_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.openFile_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class AntPopupMenu_jMenuItem11_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem11_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.jMenuItem11_mousePressed(e);
  }
}

class AntPopupMenu_jMenuItem12_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem12_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.jMenuItem12_mousePressed(e);
  }
}

class AntPopupMenu_jMenuItem13_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem13_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.jMenuItem13_mousePressed(e);
  }
}

class AntPopupMenu_jMenuItem14_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem14_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.jMenuItem14_mousePressed(e);
  }
}

class AntPopupMenu_jMenuItem15_mouseAdapter extends java.awt.event.MouseAdapter {
  AntPopupMenu adaptee;

  AntPopupMenu_jMenuItem15_mouseAdapter(AntPopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.jMenuItem15_mousePressed(e);
  }
}

